<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="box-card">
      <el-row type="flex" align="middle">
        <el-col :span="12">
          <div class="grid-content bg-purple search">
            <el-form ref="query" :model="query" label-width="100px">
              <el-form-item style="margin-bottom:0px" label="账户">
                <i slot="label" class="el-icon-search" style="font-size:20px"></i>
                <el-input v-model="query.keywords" placeholder="请输入账户"></el-input>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple-light" style="text-align:right">
            <el-button type="primary" @click="queryAsk">查询</el-button>
            <el-button @click="queryReset">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <!-- 表格 -->
    <el-card class="box-card">
      <el-table
        :data="tableData"
        :header-cell-style="{background:'#eee'}"
        border
        style="width: 100%;margin-bottom: 20px;"
        v-loading="loading"
      >
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column align="center" prop="account" label="账户"></el-table-column>
        <el-table-column align="center" prop="city" label="登录地址"></el-table-column>
        <el-table-column align="center" prop="ip" label="IP地址"></el-table-column>
        <el-table-column align="center" prop="add_time" label="登录时间"></el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="currentChange"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { loginLog } from "@/api/system";
import { parseTime } from "@/utils/index";

export default {
  name: "LoginLog",
  data() {
    return {
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //input
      query: {
        keywords: ""
      },
      tableData: [],
      loading: false
    };
  },
  mounted() {
    this.getLoginLog();
  },
  methods: {
    // 获取登录日志
    async getLoginLog() {
      this.loading = true;
      const res = await loginLog({
        page: this.currentPage,
        limit: this.pageSize,
        keywords: this.query.keywords
      });
      this.loading = false;
      console.log(res);
      this.tableData = res.data;
      this.tableData.forEach(item => {
        if (item.add_time == 0) {
          item.add_time = "-";
        } else {
          item.add_time = parseTime(item.add_time, "{y}-{m}-{d} {h}:{i}:{s}");
        }
      });
      this.total = res.count;
    },
    queryAsk() {
      this.currentPage = 1;
      this.getLoginLog();
    },
    //重置
    queryReset() {
      this.resetForm("query");
      this.total = 0;
      this.currentPage = 1;
      this.pageSize = 10;
      this.query.keywords = "";
      this.getLoginLog();
    },
    //分页改变
    currentChange(current) {
      this.currentPage = current;
      this.getLoginLog();
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.drawer = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.box-card {
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-input--medium .el-input__inner {
  width: 360px;
}
::v-deep .el-drawer.rtl {
  width: 650px !important;
  overflow: auto;
}
</style>